﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace AgFractal.Controls
{
    public class DoubleClickListBox : ListBox
    {
        DispatcherTimer timer = new DispatcherTimer();
        public DoubleClickListBox()
        {            
            timer.Tick += (s, e) => timer.Stop();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
        }

        public EventHandler<MouseButtonEventArgs> OnMouseDoubleClick { get; set; }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            if (timer.IsEnabled)
            {
                timer.Stop();
                if (OnMouseDoubleClick != null)
                    OnMouseDoubleClick(this, e);
            }
            else
                timer.Start();
        }
    }
}